package com.minyifang.wcspf.work.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.DividerItemDecoration;

import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.widgets.CustomDialog;
import com.minyifang.wcspf.work.dialog.AddToCartDialog;
import com.minyifang.wcspf.work.ui.home.activities.MainActivity;
import com.minyifang.wcspf.work.ui.user.activities.LoginActivity;
import com.minyifang.wcspf.work.ui.user.activities.SettingActivity;

import static com.minyifang.wcspf.common.base.BaseActivity.closeAllActivities;

/**
 * Created by LuoYaWei on 2018/7/26.
 */
public class Global {

    /**
     * 生成RecyclerView 分割线
     *
     * @param activity
     * @param dpHeight
     * @param color
     * @return
     */
    public static DividerItemDecoration genRecyclerViewItemDecoration(Activity activity, int dpHeight, int color) {
        DividerItemDecoration itemDecoration = new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL);
        Point outSize = new Point();
        activity.getWindow().getWindowManager().getDefaultDisplay().getSize(outSize);
        Bitmap bitmap = Bitmap.createBitmap(outSize.x, (int) (dpHeight * activity.getResources().getDisplayMetrics().density), Bitmap.Config.ARGB_8888);
        new Canvas(bitmap).drawColor(color);
        itemDecoration.setDrawable(new BitmapDrawable(bitmap));
        return itemDecoration;
    }

    /**
     * 检查是否已登录
     *
     * @param context
     * @return true 为已登录
     */
    public static boolean checkLogin(Context context) {
        if (!UserManager.getInstance().isLogin()) {
            context.startActivity(new Intent(context, LoginActivity.class));
            return false;
        }
        return true;
    }

    /**
     * 规格dialog
     *
     * @param id
     */
    public static void showSpecificationDialog(Context context, String id) {
        if (Global.checkLogin(context))
            new AddToCartDialog.Builder(context).setGoodsId(id).build().show();
    }

    public static void reLogin(Context context) {
        new CustomDialog.Builder(context)
                .setTitle("重新登录")
                .setContent("登录已过期，请重新登录！")
                .setShowCancel(false)
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        UserManager.getInstance().clearLoginData();
                        context.startActivity(new Intent(context, LoginActivity.class));
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }

    public static void exitApp(Context context, String message) {
        new CustomDialog.Builder(context)
                .setTitle("提示")
                .setContent(message)
                .setShowCancel(false)
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        System.exit(0);
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }
}
